
/**
 * 数据格式：
 * {
 *  serialno: Number,
 *  camerano: Number,
 *  status: String,
 *  statuscode: Number
 * }
 */
const cameraStatus = {
    namespaced: true,
    state: {
        items: [{
            no: 1,
            cam: 159,
            status: 6.0,
            scode: 1
        }]
    },
    getters: {
        all(state) {
            return state.items
        },

        normal(state) {
            if (state.items.length == 0) return []
            let data = state.items.filter((v) => v.statuscode == 1 ? true : false)
            return data
        },

        fault(state) {
            if (state.items.length == 0) return []
            let data = state.items.filter((v) => v.statuscode == 0 ? true : false)
            return data
        }
    },
    mutations: {
        initCameras(state, data) {
            try {
                state.items = state.items.concat(data)
                state.items = state.items.map((v, i) => v.no = i)
            } catch (err) {
                console.error(err)
            }
        },

        updateCameras(state, data) {
            try {
                for (var item of data) {
                    state.items = state.items.map(v => {
                        if (v.cam == item.cam) {
                            v.status = item.status
                            v.scode = item.scode
                        }
                    })
                }
            } catch (err) {
                console.log(err)
            }
        }
    },
    // actions: {
    //     getCameraStatus({ state, commit }) {
    //         console.log("cccc");
    //     },
    // }
}


export default cameraStatus